/**
 * 
 */
package com.desitek.thunder.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.desitek.thunder.entity.AppUser;
import com.desitek.thunder.service.IUserService;

/**
 * @author dt
 *
 */
@Controller
public class UserController {

	private IUserService userService;

	public UserController() {
	}

	@Autowired
	public UserController(IUserService userService) {
		this.userService = userService;

	}

	@RequestMapping(method = RequestMethod.GET)
	public String helloWorld(Model model) {

		model.addAttribute("msg", "hello world asadsaf");

		return "/user";
	}

	@RequestMapping(value = "/reg_user", method = RequestMethod.POST)
	public String addUser(@ModelAttribute("AppUser") AppUser user, BindingResult result) {
		userService.createUser(user);
		System.out.println(user.getUsername());

		return "redirect:users.htm";

	}

	@RequestMapping("/users")
	public ModelAndView showUsers() {

		return new ModelAndView("users", "command", new AppUser());
	}

}
